﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace CRS.Web.BackStageManagement
{
    public partial class UserInsert : VcPage
    {
        BaseDataManager m_Help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                if (String.IsNullOrEmpty(GenericContext.CurrUserCode))
                {
                    Response.Redirect("Default.aspx");
                }
                DropListBind();
            }
        }


        private void DropListBind()
        {
            var sql = "select Id,Name from VitCode_Castle_UserGroup";
            var dt = m_Help.GetDataTableBySql(sql);

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    DropDownList_UserGroup.DataSource = dt;
                    DropDownList_UserGroup.DataTextField = "Name";
                    DropDownList_UserGroup.DataValueField = "Id";
                    DropDownList_UserGroup.DataBind();
                    DropDownList_UserGroup.Items.Add(new ListItem("", ""));
                    DropDownList_UserGroup.Items.FindByText("").Selected = true;
                    dt.Dispose();
                }
            }

            sql = "select Id,Name from VitCode_Castle_Role";
            var dt2 = m_Help.GetDataTableBySql(sql);
            if (dt2 != null)
            {
                if (dt2.Rows.Count > 0)
                {
                    DropDownList_Role.DataSource = dt2;
                    DropDownList_Role.DataTextField = "Name";
                    DropDownList_Role.DataValueField = "Id";
                    DropDownList_Role.DataBind();
                    DropDownList_Role.Items.Add(new ListItem("", ""));
                    DropDownList_Role.Items.FindByText("").Selected = true;
                    dt2.Dispose();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
         
            var objUser = new User();
            IList userList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text.Trim());
            if (userList != null && userList.Count > 0)
            {
                Alert.MessageBox("用户编号已存在！");
                return;
            }
            objUser.Code = ZLTextBox_Code.Text.Trim();
            objUser.Name = ZLTextBox_AdminRealName.Text.Trim();
            objUser.Mobile = ZLTextBox_Mobile.Text.Trim();
            objUser.Email = ZLTextBox_Email.Text.Trim();
            objUser.Number = ZLTextBox_Number.Text.Trim();
            if (ZLTextBox_AdminPass.Text.Trim().Length > 0)
            {
                string StrPassword = publ.VitEnctyptString(ZLTextBox_AdminPass.Text.Trim());
                objUser.Password = StrPassword;
            }
            else
            {
                objUser.Password = publ.VitEnctyptString("123456");
            }
            if(DropDownList_UserGroup.Items.Count > 0)
            {
                if (DropDownList_UserGroup.SelectedItem.Text.Trim().Length > 0)
                {
                    string strUserGroup = DropDownList_UserGroup.SelectedItem.Value.Trim();

                    IList objList = m_Help.GetObjectList(new VitCode.Castle.Entities.UserGroup().GetType().Name, "Id", strUserGroup);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentUserGroup = (VitCode.Castle.Entities.UserGroup)objList[0];
                        objUser.UserGroup = currentUserGroup;

                    }
                }
              
            }
            bool access = m_Help.SaveOrUpdateObj(objUser);
            if (DropDownList_Role.Items.Count > 0)
            {
                if (DropDownList_Role.SelectedItem.Text.Trim().Length > 0)
                {
                    Guid guidRoleId = new Guid(DropDownList_Role.SelectedItem.Value.Trim());
                    IList objList = m_Help.GetObjectList(new User().GetType().Name, "Code", ZLTextBox_Code.Text);
                    if (objList != null && objList.Count > 0)
                    {
                        var currentUser = (User)objList[0];
                        Guid guidUserId = currentUser.Id;
                        var currentUserRoleRelation = new UserRoleRelation();
                        currentUserRoleRelation.RoleId = guidRoleId;
                        currentUserRoleRelation.UserId = guidUserId;
                        access = m_Help.SaveOrUpdateObj(currentUserRoleRelation);
                    }

                }
            }
          
            if (access)
             Page.RegisterClientScriptBlock("信息提示", "<script language='javascript'>alert('保存成功！')</script>");

        }

      
    }
}
